<?php

namespace app\commands;

use Yii;
use app\modules\Order\models\Order;
use app\modules\Product\models\Product;
use app\components\GetToken;
use yii\console\Controller;
use yii\web\NotFoundHttpException;


class NoticeController extends Controller
{

	/**
	 * 订单占位过期消息通知
	 * 
	 */
	public  function actionNotice()
	{
		// 获取所有设置占位时间 未发船
		$data = Product::find()->select("id,go_time,date_limit")->where(['status'=>1])->andWhere(['>','date_limit', 0])->andWhere(['>','go_time', date('Y-m-d')])->all();
        //获取$token
        $gettoken = new GetToken();
        $token = $gettoken->getAccessToken();
        //模板ID
        $template_id = "2DJiYcN-Nq-HQk925MPwz5TmwUEKCKzAxCHXvqdxZVo";

        $wx_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;

        $today = time();

		foreach($data as $model){

            $orders = Order::find()->where(['product_id'=>$model->id])
                                   ->andWhere(['status'=> 1])
                                   ->all();

            $t = $model->date_limit - 1;  

            if(!empty($orders)){
                foreach($orders as $ov){
                    $time = strtotime("+$t day",strtotime($ov->created_at));
                    $m = $t+1;
                    $end_time = date("Y年m月d日",strtotime("+$m day",strtotime($ov->created_at)));
                    print_r($ov->id);
                    echo "\n";
                    if($time < $today){
                        //获取openId
                        $openId = $ov->member->profile->openId;
                        $url = 'http://longyu.huoliwu.cn/member/member/order-info?id='.$ov->id;
                        $topcolor = "#FF0000";
                        $data = [
                            'first'=>[
                                'value'=>"占位到期提醒",
                                'color'=>"#A664A6"
                             ],
                            'OrderID'=>[
                                'value'=>$ov->code,
                                'color'=>'#000000'
                            ],
                            'PkgName'=>[
                                'value'=>$ov->product->name,
                                'color'=>'#000000'
                            ],
                            'TakeOffDate'=>[
                                'value'=>$ov->product->go_time,
                                'color'=>'#000000'
                            ],
                            'remark'=>[
                                'value'=>sprintf("订单联系人：%s,联系电话：%s,到期日期：%s",$ov->username,$ov->phone,$end_time),
                                'color'=>'#A664A6'
                            ],
                        ];
                        $template = [
                            'touser' => $openId,
                            'template_id' => $template_id,
                            'url' => $url,
                            'topcolor' => $topcolor,
                            'data' => $data
                        ];
                        $json_template = json_encode($template);
                        $res = $this->request_post($wx_url, urldecode($json_template));
                        $res = json_decode($res);
                        print_r($res->errmsg);
                        echo "\n";
                    } 
                    
                }
            }
			
		}
		
		exit();
	
	}

    /**
     * 订单占位过期消息通知 测试
     * 
     */
    public  function actionTest()
    {
      
        //获取$token
        $gettoken = new GetToken();
        $token = $gettoken->getAccessToken();

        //模板ID
        $template_id = "2DJiYcN-Nq-HQk925MPwz5TmwUEKCKzAxCHXvqdxZVo";

        $wx_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;

            // 获取该船期下的所有订单  时间范围内 状态为占位/同业占位
        $orders = Order::find()->where(['member_id'=>1])
                               ->andWhere(['<=', 'status', 3])
                               ->all();
           
        if(!empty($orders)){
            foreach($orders as $ov){
                //获取openId
                $openId = $ov->member->profile->openId;
                $url = 'http://longyu.huoliwu.cn/member/member/order-info?id='.$ov->id;
                $topcolor = "#FF0000";
                $data = [
                    'first'=>[
                        'value'=>"占位到期提醒",
                        'color'=>"#A664A6"
                     ],
                    'OrderID'=>[
                        'value'=>$ov->code,
                        'color'=>'#000000'
                    ],
                    'PkgName'=>[
                        'value'=>$ov->product->name,
                        'color'=>'#000000'
                    ],
                    'TakeOffDate'=>[
                        'value'=>$ov->product->go_time,
                        'color'=>'#000000'
                    ],
                    'remark'=>[
                        'value'=>sprintf("订单联系人：%s,联系电话：%s",$ov->username,$ov->phone),
                        'color'=>'#A664A6'
                    ],
                ];
                $template = [
                    'touser' => $openId,
                    'template_id' => $template_id,
                    'url' => $url,
                    'topcolor' => $topcolor,
                    'data' => $data
                ];
                $json_template = json_encode($template);
                $res = $this->request_post($wx_url, urldecode($json_template));
                $res = json_decode($res);
                print_r($res->errmsg);
                echo "\n";
                //print_r($template);
            }
        }
    
        exit();
    
    }

    //发送post请求
    private function request_post($url = '', $param = '')
    {
        if (empty($url) || empty($param)) {
            return false;
        }
        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init(); //初始化curl
        curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch); //运行curl
        curl_close($ch);
        return $data;
    }
 



}
